<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-4-19
 * Time: 下午3:23
 */

namespace Lib;


use Lib\Db\MySQLi;

class Factory
{
    //对底层接口做封装,然后让业务来调用此封装好的接口,防止底层接口的变动造成业务层的实例化连带变动
    static function createDataBase(){
        $db = Database::getInstance();
        return $db;
    }

    static function getMapping($id){
        //普通模式
//        $mapping = (new Mapping($id));
        //注册器模式
        $key = 'mapping_'.$id;
        $mapping = Register::get($key);
        if (!$mapping){
            $mapping = new Mapping($id);
            Register::set($key,$mapping);
        }

        return $mapping;
    }

    static function getUser($id){
        //注册器模式
        $key = 'user_'.$id;
        $mapping = Register::get($key);
        if (!$mapping){
            $mapping = new User($id);
            Register::set($key,$mapping);
        }

        return $mapping;
    }

    static function getDataBase($type = 'master'){
        $db = new MySQLi();
        if ($type == 'slave'){
            $db->connect('127.0.0.1','root','st826115');
        }else{
            $db->connect('localhost','root','st826115');
        }
        return $db;
    }
}